package banco.composEstoque;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import utilidades.FormatarData;
import banco.Banco;
import dominio.Cliente;
import dominio.composEstoque.Cons_Imob;
import dominio.composEstoque.Demonstrativo_Cons_Imob;
import fachada.composEstoque.Fachada_Cliente_Composestoque;

public class Cliente_Composestoque_DAO implements Fachada_Cliente_Composestoque {

	/* (non-Javadoc)
	 * @see banco.composEstoque.Fachada_Cliente_Composestoque#insertCliente_Composestoque(dominio.Cliente, java.lang.String)
	 */
	@SuppressWarnings("finally")
	public void insertCliente_Composestoque(Cliente c, String filial) throws ClassNotFoundException, IOException {
	        
	        Connection conn = null;
	        PreparedStatement stmt = null;
	        ResultSet rs = null;
	        
	        try {
	            conn = Banco.getConexaoEXTRATOPAES();
	            stmt = conn.prepareStatement("insert into cliente_composestoque values (?, ?)");
	            stmt.setString(1, c.getCnpj().trim());
	            stmt.setString(2, filial);
	            stmt.executeUpdate();
	        }
	        catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        
	        catch (Exception e) {
	        	e.printStackTrace();
	        }
	        finally {
		        Banco.closeConexao(conn, rs, stmt);
	        }
	    }

	/* (non-Javadoc)
	 * @see banco.composEstoque.Fachada_Cliente_Composestoque#selectFilial(dominio.Cliente)
	 */
	@SuppressWarnings("finally")
	public Collection selectFilial(Cliente c) throws IOException {
		
		Connection conn = null;
	    PreparedStatement stmt = null;
	    ResultSet rs = null;
	    Collection<String> res = new ArrayList<String>();
	    
	    try {
	        conn = Banco.getConexaoEXTRATOPAES();
	        stmt = conn.prepareStatement("select * from cliente_composestoque where cnpj_cliente = ?");
	        stmt.setString(1, c.getCnpj().trim());
	        rs = stmt.executeQuery();
	        while (rs.next()){
	        	String filial = rs.getString("filial");
	        	res.add(filial);
	        }
	    }
	    catch (SQLException ex) {
	        ex.printStackTrace();
	    }
	    finally {
	        Banco.closeConexao(conn, rs, stmt);
	        return res;
	    }
	    
	}
	
}
